//封装
package com.encap;

public class Encapsulation01 {
    public static void main(String[] args) {
        Person person = new Person();
        person.setName("Lily");
        person.setAge(26);
        person.setSalary(8000.00);
        //String name = person.getName();
        System.out.println( person.info());
        System.out.println(person.getSalary("123456"));

        //如果自己使用构造器指定属性
        Person ada = new Person("ada", 18, 3300);
        System.out.println(ada.info());
    }
}

class Person{
    public String name;// 名字公开
    private int age; //age 私有化
    private double salary; //薪水是隐私

    //构造器
    public Person() {

    }
    //有三个属性的构造器
    public Person(String name,int age,double salary) {
        this.setName(name);
        this.setAge(age);
        this.setSalary(salary);

        setName(name);
        setAge(age);
        setSalary(salary);

    }

    public  void  setName(String name){
        this.name = name;
    }

    //自己写setXxx 和 getXxx 太慢了。
    //快捷键  command+n

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        //增加逻辑判断
        if(age >= 1 && age <= 120){
            this.age = age;
        }else{
            System.out.println("你设置的年龄不对，需要在（1-120岁），默认年龄18岁");
            this.age = 18;
        }

    }

    public double getSalary(String pwd) {
        //增加密码校验
        if(pwd == "123"){
            return salary;
        }else{
            System.out.println("你获取工资密码不对，默认工资 3000");
            this.salary = 3000;
            return   this.salary;
        }

    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    //写一个方法，返回属性信息
    public String info(){
        return  "信息为 name="+name+" age="+age+" salary="+salary;
    }
}
